import {
	createStore
} from "vuex";
import createPersistedState from "vuex-persistedstate";
import {
	ElMessageBox
} from 'element-plus';

let persistedStatePlugin = null

try {
	if (typeof window !== 'undefined') {
		persistedStatePlugin = createPersistedState({
			storage: window.localStorage
		})
	}
} catch (error) {
	console.warn('localStorage is not accessible:', error)
	// 可以选择使用内存存储或其他降级方案
	// 无痕/隐私模式下，存储可能被完全禁用
	ElMessageBox.confirm(
			"无痕/隐私模式下，存储可能被完全禁用",
			'提示', {
				confirmButtonText: '确认',
				showCancelButton: false,
				type: 'warning',
			}
		)

}


// Vuex.Store 构造器选项
const store = createStore({
	// 为了不和页面或组件的data中的造成混淆，state中的变量前面建议加上$符号
	state: {
		// 用户信息
		$userInfo: {
			id: ''
		},
		uid: '',
		header: {
			'user': ''
		},
		num: 0,
		url: 'https://www.yaojiapx.cn/weixin',
		// url: 'http://localhost:8080//weixin',
		socketurl: 'wss://www.yaojiapx.cn/weixin',
		execlurl: 'https://www.yaojiapx.cn/',
		icon: 'play',
		musicnum: 0,
		classname: '',
		classid: '',
		imageurl: 'https://www.yaojiapx.cn/image/',
		pyurl: 'https://www.yaojiapx.cn',
		URL: "wss://nls-gateway.cn-shanghai.aliyuncs.com/ws/v1",
		AKID: '',
		AKKEY: '',
		APPKEY: '',
		token: '',
		visitTotal: 161580,
		forksCount: 16561,
		insertTimeSet: [], //打卡时间集合
		$tabbar: {
			status: true,
		},
		activeIndex: "1", //右侧点击选项
		member: '11',
		userRecord: '', //自身功能定制
		settings: {
			voiceName: '艾琪，温柔女声',
			voice: 'zh-CN-XiaoxiaoNeural', //发音人
			volume: 50, //音量
			speech_rate: 0, //语速
			pitch_rate: 0, //语调
			popupShow: false,
			cascaderArray: []
		},
		routerViewShow: true, //登录界面
		qrUrl: '', //login
		classNameId: '',
		organizationId: '',
		organizationType: '', //机构类型
		organizationIndex: 0, //机构下标
	},
	mutations: {
		setRouterViewShow(state, routerViewShow) {
			state.routerViewShow = routerViewShow;
		},
		setuserId(state, userId) {
			state.$userInfo.id = userId;
			state.header.user = userId;
		},
		setUid(state, uid) {
			state.uid = uid;
		},
		setClassNameId(state, classNameId) {
			state.classNameId = classNameId;
		},
		setOrganizationId(state, organizationId) {
			state.organizationId = organizationId;
		},
		setMember(state, member) {
			state.member = member;
		}

	},
	plugins: persistedStatePlugin ? [persistedStatePlugin] : [],
})

export default store